package com.itheima.easy.task;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.itheima.easy.dto.BillDto;
import com.itheima.easy.service.BillService;
import com.itheima.easy.service.ElderService;
import com.itheima.easy.vo.retreat.ElderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 账单管理
 *
 * @author wxb
 * @version 1.0
 */
@Slf4j
@Component
public class BillJob {

    @Resource
    BillService billService;

    @Resource
    ElderService elderService;

    /**
     * 生成月度账单
     */
    @Scheduled(cron = "0 0 0 1 * ?")
    public void contractJob() {
        List<ElderVo> elderVos = elderService.selectList();
        String format = LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy-MM");
        for (ElderVo elder : elderVos) {
            BillDto billDto = new BillDto();
            billDto.setElderId(elder.getId());
            billDto.setBillMonth(format);
            billService.createProjectBill(billDto);
        }
    }
}
